//
//  TodayView.m
//  Water
//
//  Created by chenguibang on 2017/11/26.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "TodayView.h"
#import <Masonry/Masonry.h>
@implementation TodayView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

#pragma mark - Private

- (void)setupViews {
    
    
    
    //star
    self.starsView = [[YMStarsView alloc] init];
    self.starsView.delegate = self;
    self.starsView.dateSource = self;
    [self addSubview:self.starsView];
    [self.starsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.equalTo(self.starsView.superview);
        make.top.equalTo(self.starsView.superview).with.offset(0.0f);
    }];
    [self.starsView showStars];
    
    
    self.weatherInfoLabel = [[UILabel alloc]init];
    [self addSubview:self.weatherInfoLabel];
    [self.weatherInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(40);
        make.width.equalTo(@300);
    }];
    [self.weatherInfoLabel setPreferredMaxLayoutWidth:300];
    self.weatherInfoLabel.numberOfLines = 0;
    self.weatherInfoLabel.textColor = [UIColor whiteColor];
    
    CGFloat defaultValue = 0.0f;
    CGRect frame = CGRectMake((SCREEN_WIDTH - 220.0f) * 0.5f, 130.0f, 220.0f, 220.0f);
    self.circleAnimationView = [[YMPowerDashboard alloc] initWithFrame:frame];
    self.circleAnimationView.animationInterval = 1.5f;
    self.circleAnimationView.strokeColor = [UIColor whiteColor];
    [self.circleAnimationView setPercent:defaultValue
                                animated:YES];
    self.circleAnimationView.title = @"今日饮水";
    self.circleAnimationView.subTitle = [NSString stringWithFormat:@"剩余%.1fml",0.6 * 55];
    [self addSubview:self.circleAnimationView];
    [self.circleAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(220.0f);
        make.centerX.equalTo(self);
        make.top.mas_equalTo(220.0f);
    }];
    

    
    
    self.drink = [[UIButton alloc]init];
    [self addSubview:self.drink];
    self.drink.hidden = YES;
    [self.drink mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.circleAnimationView);
        make.right.equalTo(self.circleAnimationView);
        make.top.equalTo(self.circleAnimationView.mas_bottom).offset(40);
        make.height.equalTo(@40);
    }];
    [self.drink setTitle:@"喝杯水" forState:UIControlStateNormal];
    [self.drink addTarget:self action:@selector(drink:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(60, 60);
    self.waterCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layout];
    [self addSubview:self.waterCollectionView];
    [self.waterCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-60);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.equalTo(@60);
    }];
    
    self.waterCollectionView.backgroundColor = [UIColor clearColor];
    
    
  
    
    self.recordBtn = [[UIButton alloc]init];
    [self addSubview:self.recordBtn];
    [self.recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(44);
        make.right.equalTo(self).offset(-40);
    }];
    [self.recordBtn setTitle:@"记录" forState:UIControlStateNormal];
    
    self.makePlanBtn = [[UIButton alloc]init];
    [self addSubview:self.makePlanBtn];
    [self.makePlanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.recordBtn.mas_bottom).offset(20);
        make.right.equalTo(self).offset(-40);
    }];
    [self.makePlanBtn setTitle:@"定制计划" forState:UIControlStateNormal];
    
    self.drinkTargetView = WaterXIB(@"DrinkTarget");
    self.drinkTargetView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.drinkTargetView];
    [self.drinkTargetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.makePlanBtn.mas_bottom).offset(40);
        make.right.equalTo(self).offset(-44);
        make.height.equalTo(@25);
        make.width.equalTo(@150);
    }];
}


#pragma mark - StarsViewDelegate

- (CGRect)centerRectForStarsView:(YMStarsView *)starsView {
    return CGRectMake((SCREEN_WIDTH - 260.0f) * 0.5f, 0.0f, 260.0f, 260.0f);
}

- (CGFloat)verticalPaddingForStarsView:(YMStarsView *)starsView {
    return 10.0f;
}

- (CGFloat)horizontalPaddingForStarsView:(YMStarsView *)starsView {
    return 10.0f;
}

#pragma mark - StarsViewDataSource

- (NSArray<NSNumber *> *)starRadiusesForStarsView:(YMStarsView *)starsView {
    return @[@4, @5, @6, @2];
}

- (NSUInteger)starCountForStarsView:(YMStarsView *)starsView {
    return 15;
}

#pragma mark - Handlers


- (void)drink:(UIButton *)sender{
    progress += 0.1;
    [self.circleAnimationView setPercent:progress
                                animated:YES];
   self.circleAnimationView.subTitle = [NSString stringWithFormat:@"剩余%.1fml",0.2];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
